package cn.zyy.hotel.controller.admin;

import cn.zyy.hotel.entity.RoomType;
import cn.zyy.hotel.service.RoomTypeService;
import cn.zyy.hotel.utils.Result;
import cn.zyy.hotel.utils.SystemConstant;
import cn.zyy.hotel.vo.RoomTypeVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/roomType")
public class RoomTypeController {

    @Resource
    private RoomTypeService roomTypeService;
    @Resource
    private RedisTemplate redisTemplate;

    //查询房型列表
    @RequestMapping("/list")
    public Result list(RoomTypeVO roomTypeVo){
        //设置分页信息
        PageHelper.startPage(roomTypeVo.getPage(),roomTypeVo.getLimit());
        List<RoomType> roomTypeList;
        if (redisTemplate.hasKey("roomTypeList")){
            roomTypeList = redisTemplate.opsForList().range("roomTypeList",0,-1);
        }else {
            //调用查询房型列表的方法
            roomTypeList = roomTypeService.selectRoomTypeList(roomTypeVo);
            redisTemplate.opsForList().rightPushAll("roomTypeList",roomTypeList);
        }
        //创建分页对象
        PageInfo<RoomType> pageInfo = new PageInfo<RoomType>(roomTypeList);
        return Result.builder().code(0).count(pageInfo.getTotal()).data(pageInfo.getList()).build();
    }

    //添加房型
    @RequiresPermissions("roomtype:add")
    @RequestMapping("/addRoomType")
    public Object addRoomType(RoomType roomType){
        Map<String,Object> map = new HashMap<String,Object>();
        if(roomTypeService.addRoomType(roomType)>0){
            //成功
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"添加成功");
            if (redisTemplate.hasKey("roomTypeList")){
                redisTemplate.delete("roomTypeList");
            }
        }else{
            //失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"添加失败");
        }
        return map;
    }

    //修改房型
    @RequiresPermissions("roomtype:edit")
    @RequestMapping("/updateRoomType")
    public Object updateRoomType(RoomType roomType){
        Map<String,Object> map = new HashMap<String,Object>();
        if(roomTypeService.updateRoomType(roomType)>0){
            //成功
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"修改成功");
            if (redisTemplate.hasKey("roomTypeList")){
                redisTemplate.delete("roomTypeList");
            }
        }else{
            //失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"修改失败");
        }
        return map;
    }

    //查询当前房型下是否存在房间
    @RequestMapping("/checkRoomTypeHasRoom")
    public Object checkRoomTypeHasRoom(Integer id){
        Map<String, Object> map = new HashMap<String, Object>();
        //调用查询房型房间的方法
        if (roomTypeService.getRoomCountByRoomTypeId(id)>0){
            map.put(SystemConstant.EXIST,true);
            map.put(SystemConstant.MESSAGE,"该房型下有房间，无法删除");
        }else {
            map.put(SystemConstant.EXIST,false);
        }
        return map;
    }

    //删除房型
    @RequiresPermissions("roomtype:delete")
    @RequestMapping("/deleteById")
    public Object deleteById(Integer id){
        Map<String, Object> map = new HashMap<String, Object>();
        //调用查询菜单的方法
        if (roomTypeService.deleteById(id)>0){
            //成功
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"删除成功");
            if (redisTemplate.hasKey("roomTypeList")){
                redisTemplate.delete("roomTypeList");
            }
        }else {
            //失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"删除失败");
        }
        return map;
    }

    //查询所有房间类型
    @RequestMapping("/findAll")
    public Object findAll(){
        List<RoomType> roomTypeList;
        if (redisTemplate.hasKey("roomTypeList")){
            roomTypeList = redisTemplate.opsForList().range("roomTypeList",0,-1);
        }else {
            //调用查询房型列表的方法
            roomTypeList = roomTypeService.selectRoomTypeList(null);
            redisTemplate.opsForList().rightPushAll("roomTypeList",roomTypeList);
        }
        return roomTypeList;
    }


}
